from django.http import JsonResponse
from rest_framework import status
from rest_framework.views import APIView
from .models import Message
from .serializers import MessageSerializer, BlogMessageSerializer


# 提交评论接口
class SubmitMessageView(APIView):
    def post(self, request, format=None):
        # 将 request.data 转换为可修改的字典
        data = request.data.copy()  # 使用 .copy() 创建可修改的副本
        data['status'] = 0  # 初始化状态为 "审核中"

        serializer = MessageSerializer(data=data)
        if serializer.is_valid():
            serializer.save()
            return JsonResponse(
                {"code": "200", "data": serializer.data, "message": "提交成功", "ok": "true"},
                status=status.HTTP_201_CREATED,
            )
        return JsonResponse(
            {"code": "400", "data": serializer.errors, "message": "数据验证失败", "ok": "false"},
            status=status.HTTP_400_BAD_REQUEST,
        )


# 博客端查询留言接口
class BlogMessageListView(APIView):
    def get(self, request, format=None):
        messages = Message.objects.filter(status=1)  # 只返回审核通过的留言
        serializer = BlogMessageSerializer(messages, many=True)
        return JsonResponse(
            {"code": "200", "data": serializer.data, "message": "查询成功", "ok": "true"},
            status=status.HTTP_200_OK,
        )


# 管理端查询留言接口
class AdminMessageListView(APIView):
    def get(self, request, format=None):
        messages = Message.objects.all()  # 返回所有留言
        serializer = MessageSerializer(messages, many=True)
        return JsonResponse(
            {"code": "200", "data": serializer.data, "message": "查询成功", "ok": "true"},
            status=status.HTTP_200_OK,
        )


# 修改留言状态接口
class UpdateMessageStatusView(APIView):
    def put(self, request, pk, format=None):
        try:
            message = Message.objects.get(pk=pk)
        except Message.DoesNotExist:
            return JsonResponse(
                {"code": "404", "data": None, "message": "留言不存在", "ok": "false"},
                status=status.HTTP_404_NOT_FOUND,
            )

        new_status = request.data.get('status')
        if new_status is None:
            return JsonResponse(
                {"code": "400", "data": None, "message": "状态值不能为空", "ok": "false"},
                status=status.HTTP_400_BAD_REQUEST,
            )

        message.status = new_status
        message.save()
        return JsonResponse(
            {"code": "200", "data": {"status": new_status}, "message": "状态更新成功", "ok": "true"},
            status=status.HTTP_200_OK,
        )


class DeleteMessageView(APIView):
    def delete(self, request, pk, format=None):
        try:
            message = Message.objects.get(pk=pk)
        except Message.DoesNotExist:
            return JsonResponse(
                {"code": "404", "data": None, "message": "留言不存在", "ok": "false"},
                status=status.HTTP_404_NOT_FOUND,
            )

        message.delete()
        return JsonResponse(
            {"code": "200", "data": None, "message": "删除成功", "ok": "true"},
            status=status.HTTP_200_OK,
        )